// pages/comparison/index.js
import loginFn from '../../utils/login.js'
import urls from '../../utils/urls.js'
var app = getApp()
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		list: [], //课程
		school: [], //机构
		teachers: [] //教师
	},

	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		console.log('options:',options);
		this.getcourseFn()
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {

	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	},
	getcourseFn: function() { //课程信息
		var compares = wx.getStorageSync('compare');
		console.log(compares)
		var loginInfo = loginFn()
		var that = this;
		loginInfo.courseId = compares
		wx.request({
			url: urls.courseInfo,
			data: loginInfo,
			success: function(res) {
				console.log(res)
				if (res.data.code == '0') {
					var schools = [] //需要比较的机构id
					var price = [] //现价
					var oldPrice = [] //原价
					var totalClassHours = [] //总课时
					var buyCount = [] //已售数量
					var totalScore = [] //评分
					var want = [] //想学人数
					var learned = [] //想学人数
					var list = res.data.data
					for (var i = 0; i < list.length; i++) {
						schools.push(list[i].schoolId)
						price[i] = list[i].price
						list[i].pricemax = false
						oldPrice[i] = list[i].oldPrice
						list[i].oldPricemax = false
						totalClassHours[i] = list[i].totalClassHours
						list[i].totalClassHoursmax = false
						buyCount[i] = list[i].buyCount
						list[i].buyCountmax = false
						want[i] = list[i].want
						list[i].wantmax = false
						learned[i] = list[i].learned
						list[i].learnedmax = false
						totalScore[i] = list[i].totalScore
						list[i].totalScoremax = false
					}
					list[that.getminFn(price)].pricemax = true
					list[that.getminFn(oldPrice)].oldPricemax = true
					list[that.getmaxFn(totalClassHours)].totalClassHoursmax = true
					list[that.getmaxFn(buyCount)].buyCountmax = true
					list[that.getmaxFn(want)].wantmax = true
					list[that.getmaxFn(learned)].learnedmax = true
					list[that.getmaxFn(totalScore)].totalScoremax = true

					that.setData({
						list: list
					})
					that.getschoolFn(schools)
				};

			}
		})
		//教师对比信息
		this.getteacherFn(loginInfo)
	},
	getschoolFn: function(schoolId) { //获取学校数据
		var that = this;
		var loginInfo = loginFn()
		loginInfo.schoolId = schoolId
		wx.request({
			url: urls.schoolUrl,
			method: 'POST',
			data: loginInfo,
			success: function(res) {
				if (res.data.code == '0') {
					var area = [] //面积
					var roomCount = [] //教室数量
					var teacherCount = [] //教室数量
					var schoolScore = [] //评分
					var want = [] //想学人数
					var learned = [] //想学人数
					var list = res.data.data
					for (var i = 0; i < list.length; i++) {
						area[i] = list[i].area
						list[i].areamax = false
						roomCount[i] = list[i].roomCount
						list[i].roomCountmax = false
						teacherCount[i] = list[i].teacherCount
						list[i].teacherCountmax = false
						want[i] = list[i].want
						list[i].wantmax = false
						learned[i] = list[i].learned
						list[i].learnedmax = false
						schoolScore[i] = list[i].schoolScore
						list[i].schoolScoremax = false
						//办学形式
						for (var j = 0; j < app.globalData.allselect.schoolSort.length; j++) {
							if (app.globalData.allselect.schoolSort[j].schoolSortId == list[i].schoolSortId) {
								list[i].schoolSortId = app.globalData.allselect.schoolSort[j].sortName
							};
						}
						//机构规模
						for (var j = 0; j < app.globalData.allselect.schoolScale.length; j++) {
							if (app.globalData.allselect.schoolScale[j].schoolScaleId == list[i].schoolScaleId) {
								list[i].schoolScaleId = app.globalData.allselect.schoolScale[j].schoolScale
							};
						}
						//专业方向
						for (var j = 0; j < app.globalData.allselect.subject.length; j++) {
							if (app.globalData.allselect.subject[j].subjectId == list[i].major) {
								list[i].major = app.globalData.allselect.subject[j].subject
							};
						}
					}
					list[that.getmaxFn(area)].areamax = true
					list[that.getmaxFn(roomCount)].roomCountmax = true
					list[that.getmaxFn(teacherCount)].teacherCountmax = true
					list[that.getmaxFn(want)].wantmax = true
					list[that.getmaxFn(learned)].learnedmax = true
					list[that.getmaxFn(schoolScore)].schoolScoremax = true
					console.log(list)
					that.setData({
						school: list
					})
				}
			}
		})
	},
	getmaxFn: function(data) { //获取最大是第几个
		var max = parseFloat(data[0]);
		var maxidx = 0
		for (var i = 1; i < data.length; i++) {
			if (parseFloat(data[i]) > max) {
				maxidx = i;
				max = parseFloat(data[i])
			}
		}
		return maxidx;
	},
	getminFn: function(data) { //获取最大是第几个
		var min = parseFloat(data[0]);
		var minidx = 0
		for (var i = 1; i < data.length; i++) {
			if (parseFloat(data[i]) < min) {
				minidx = i;
				min = parseFloat(data[i])
			}
		}
		return minidx;
	},
	getteacherFn: function(loginInfo) { //教师对比信息
		var that = this;
		wx.request({
			url: urls.teacherUrl,
			method: 'POST',
			data: loginInfo,
			success: function(res) {
				console.log(res)
				if (res.data.code == '0') {
					var list = res.data.data
					var date = new Date;
					var year = date.getFullYear();
					var years = [] //教龄
					var likeCount = [] //教龄
					var want = [] //想学人数
					var learned = [] //想学人数
					var finalScore = [] //评分
					for (var i = 0; i < list.length; i++) {
						console.log(list[i])
						if (list[i]) {
							list[i].bornYear = year - list[i].bornYear //年龄
							years[i] = list[i].years
							list[i].yearsmax = false
							likeCount[i] = list[i].likeCount
							list[i].likeCountmax = false
							want[i] = list[i].want
							list[i].wantmax = false
							learned[i] = list[i].learned
							list[i].learnedmax = false
							finalScore[i] = list[i].finalScore
							list[i].finalScoremax = false
							for (var j = 0; j < list[i].teacherLabel.length; j++) {
								if (list[i].teacherLabel[j].labelId == '9') {
									list[i].famous = true
								};
								if (list[i].teacherLabel[j].labelId == '7') {
									list[i].famousexper = true
								};
								if (list[i].teacherLabel[j].labelId == '10') {
									list[i].prize = true
								};
							}
						} else {
							years[i] = -1
							likeCount[i] = -1
							want[i] = -1
							learned[i] = -1
							finalScore[i] = -1
						};

					}
					if (list[that.getmaxFn(years)]) {
						list[that.getmaxFn(years)].yearsmax = true
					};
					if (list[that.getmaxFn(likeCount)]) {
						list[that.getmaxFn(likeCount)].likeCountmax = true
					};
					if (list[that.getmaxFn(want)]) {
						list[that.getmaxFn(want)].wantmax = true
					};
					if (list[that.getmaxFn(learned)]) {
						list[that.getmaxFn(learned)].learnedmax = true
					};
					if (list[that.getmaxFn(finalScore)]) {
						list[that.getmaxFn(finalScore)].finalScoremax = true
					};
					that.setData({
						teachers: list
					})
				};
			}
		})
	},
	closeFn: function(e) { //删除课程
		console.log(e)
		var idx = e.currentTarget.dataset.idx
		var list = this.data.list
		var school = this.data.school
		var teachers = this.data.teachers
		list.splice(idx, 1)
		school.splice(idx, 1)
		teachers.splice(idx, 1)
		var compares = wx.getStorageSync('compare');
		compares.splice(idx, 1)
		wx.setStorageSync('compare', compares)
		this.setData({
			list: list,
			school: school,
			teachers: teachers
		})
	},
	gotobuyFn: function(e) { //购买课程
		console.log(e)
		wx.navigateTo({
			url: '/pages/order/index?courseId=' + e.currentTarget.dataset.courseid,
		})
	}
})
